package com.example.foods.service.impl;

import com.example.foods.data.FoodsMapper;
import com.example.foods.domain.AdminInfo;
import com.example.foods.domain.FoodsInfo;
import com.example.foods.entity.FoodCategory;
import com.example.foods.entity.FoodDetail;
import com.example.foods.entity.FoodTag;
import com.example.foods.entity.Foods;
import com.example.foods.service.FoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service

@Transactional
public class FoodsServiceImpl implements FoodsService {

    @Resource
    private FoodsMapper foodsMapper;

    @Override
    public boolean addFoods(FoodsInfo foodsInfo, AdminInfo adminInfo) {
        Foods foods = this.foodsMapper.findByFoodName(foodsInfo.getFoodName());
        if (foods != null) {
            throw new RuntimeException("该菜品已经存在，不能录入同名菜品!");
        }
        foods = new Foods();
        foods.setFoodName(foodsInfo.getFoodName());
        foods.setPrice(foodsInfo.getPrice());
        foods.setRemarks(foodsInfo.getRemarks());
        foods.setBaseImg(foodsInfo.getBaseImg());
        foods.setDelFlag((short) 0);
        foods.setCreator(adminInfo.getAdminId());

        foods.setCreateDate(new Date());
        //保存：菜品信息表，然后使用菜品编号作为外键给菜品详情表，菜品分类表，菜品标签表插入记录
        int count = this.foodsMapper.saveFoods(foods);
        if (count > 0) {
            //详情
            List<FoodDetail> detailList = new ArrayList<>();
            if (foodsInfo.getImgPath() != null && foodsInfo.getImgPath().size() > 0) {
                for (String imgPath : foodsInfo.getImgPath()) {
                    FoodDetail foodDetail = new FoodDetail();
                    foodDetail.setFoodId(foods.getFoodId());
                    foodDetail.setImgPath(imgPath);
                    detailList.add(foodDetail);
                }
                this.foodsMapper.saveFoodDetails(detailList);
            }
            //所属类别
            List<FoodCategory> foodCategoryList = new ArrayList<>();
            if (foodsInfo.getCatId() != null && foodsInfo.getCatId().size() > 0) {
                for (Integer catId : foodsInfo.getCatId()) {
                    FoodCategory foodCategory = new FoodCategory();
                    foodCategory.setFoodId(foods.getFoodId());
                    foodCategory.setCatId(catId);
                    foodCategoryList.add(foodCategory);
                }
                this.foodsMapper.saveFoodCategories(foodCategoryList);
            }
            //标签
            List<FoodTag> foodTagList = new ArrayList<>();
            if (foodsInfo.getTagId() != null && foodsInfo.getTagId().size() > 0) {
                for (Integer tagId : foodsInfo.getTagId()) {
                    FoodTag foodTag = new FoodTag();
                    foodTag.setFoodId(foods.getFoodId());
                    foodTag.setTagId(tagId);
                    foodTagList.add(foodTag);
                }
                this.foodsMapper.saveFoodTags(foodTagList);
            }
        }
        return count > 0;
    }

    @Override
    public List<FoodsInfo> findall() {
        List<Foods> foods = this.foodsMapper.findAll();
        System.out.println(foods);
        List<FoodsInfo> infos = new ArrayList<>();
        FoodsInfo info;
        for (Foods food : foods) {
            info = new FoodsInfo(); // 注意这里应该是 FoodsInfo
            BeanUtils.copyProperties(food, info); // 复制 Foods 对象的属性到 FoodsInfo 对象
            infos.add(info);
        }
        return infos;
    }

    @Override
    public int dleteflowers(int id) {
        int flowers = this.foodsMapper.deleteflowers(id);
        return flowers;
    }
}
